﻿using System;
using System.Collections.Generic;
using System.Text;

namespace PlutoStudio.Runtime.Session
{
    public abstract class SessionBase : ISession
    {
        public const string SessionOverrideContextKey = "PlutoStudio.Runtime.Session.Override";


        public abstract int? UserId { get; }

        public abstract int? ImpersonatorUserId { get; }

        protected SessionOverride OverridedValue => SessionOverrideScopeProvider.GetValue(SessionOverrideContextKey);
        protected IAmbientScopeProvider<SessionOverride> SessionOverrideScopeProvider { get; }

        protected SessionBase(IAmbientScopeProvider<SessionOverride> sessionOverrideScopeProvider)
        {
            SessionOverrideScopeProvider = sessionOverrideScopeProvider;
        }

        public IDisposable Use(int? userId)
        {
            return SessionOverrideScopeProvider.BeginScope(SessionOverrideContextKey, new SessionOverride(userId));
        }
    }
}
